package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.DateFormatUtil;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserManager {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private VideoService videoService;

    @Reference
    private MovementService movementService;

    @Reference
    private CommentService commentService;

    //查询用户列表
    public PageBeanVo findUserList(Integer pageNum, Integer pageSize) {
        //1. 调用userService分页列表查询
        PageBeanVo pageBeanVo = userInfoService.findUserList(pageNum,pageSize);

        //2. 封装返回结果
        return pageBeanVo;
    }

    //查询用户详情
    public UserInfo findUserInfo(Long userId) {
        return userInfoService.findById(userId);
    }

    //查询用户视频列表
    public PageBeanVo findUserVideoList(Long uid, Integer pageNum, Integer pageSize) {
        //1. 根据uid调用service分页查询视频列表
        PageBeanVo pageBeanVo = videoService.findByUserId(uid,pageNum,pageSize);

        //2. 组装返回结果videoVo
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        List<VideoVo> videoVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                VideoVo videoVo = new VideoVo();

                //3-1 设置视频发布人信息
                UserInfo userInfo = userInfoService.findById(video.getUserId());

                BeanUtils.copyProperties(userInfo, videoVo);

                //3-2 设置视频信息
                BeanUtils.copyProperties(video, videoVo);
                //3-3 设置上面没有赋值成功的
                videoVo.setId(video.getId().toHexString());//设置视频id
                videoVo.setCover(video.getPicUrl());//视频封面
                videoVo.setSignature(video.getText());//视频签名

                videoVoList.add(videoVo);
            }
        }

        pageBeanVo.setItems(videoVoList);

        return pageBeanVo;
        }


        //分页查询动态
        public PageBeanVo findMovementList(Long uid, Integer stateInt, Integer pageNum, Integer pageSize) {
            //1. 调用service查询动态
            PageBeanVo pageBeanVo = movementService.findMovementList(uid, stateInt, pageNum, pageSize);

            //2. 封装返回结果
            //2-1 获取到返回值中的movement列表
            List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
            List<MovementVo> movementVoList = new ArrayList<>();//临时集合

            if (CollectionUtil.isNotEmpty(movementList)) {
                for (Movement movement : movementList) {
                    //2-2 创建movementVo,来封装前端需要的返回值对象中的集合部分
                    MovementVo movementVo = new MovementVo();

                    //2-3 设置movementVo中动态发布者的信息
                    UserInfo userInfo = userInfoService.findById(movement.getUserId());//根据动态发布人id查询其详细信息
                    movementVo.setUserInfo(userInfo);

                    //2-4 设置movementVo中动态的信息
                    movementVo.setMovement(movement);

                    //2-5 这里有个小坑,前端要的是一个格式化之后的日期类型
                    movementVo.setCreateDate(DateUtil.format(new Date(movement.getCreated()), "yyyy-MM-dd HH:mm:ss"));

                    //2-6 将movementVo放入临时集合
                    movementVoList.add(movementVo);
                }
            }

            //3 替换分页对象中的items(List<Movement>---->List<MovementVo>)
            pageBeanVo.setItems(movementVoList);

            //4 返回结果
            return pageBeanVo;
        }

    //查询用户动态详情
    public MovementVo findMovementDetail(String movementId) {
        //1. 调用service根据动态id查询动态信息
        Movement movement = movementService.findMyMovementById(movementId);

        //2. 组装返回结果
        MovementVo movementVo = new MovementVo();
        UserInfo userInfo = userInfoService.findById(movement.getUserId());//根据动态发布人id查询其详细信息
        movementVo.setUserInfo(userInfo);
        movementVo.setMovement(movement);
        movementVo.setCreateDate(DateUtil.format(new Date(movement.getCreated()), "yyyy-MM-dd HH:mm:ss"));
        return movementVo;
    }

    //查询用户动态评论列表
    public PageBeanVo findMovementCommentList(String movementId, Integer pageNum, Integer pageSize) {
        //1. 调用service查询列表
        PageBeanVo pageBeanVo = commentService.findCommentByMovement(movementId,pageNum,pageSize);

        //2. 组装返回结果
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        List<CommentVo> commentVoList = new ArrayList<>();//临时集合

        if(CollectionUtil.isNotEmpty(commentList)){
            for (Comment comment : commentList) {
                //3-1 设置评论信息
                CommentVo commentVo = new CommentVo();
                commentVo.setId(comment.getId().toHexString());//评论id
                commentVo.setContent(comment.getContent());//评论内容
                commentVo.setCreateDate(DateFormatUtil.format(new Date(comment.getCreated())));//评论的发布时间

                //3-2 设置评论发布人的信息
                Long userId = comment.getUserId();//评论人id
                UserInfo userInfo = userInfoService.findById(userId);//评论人信息
                commentVo.setAvatar(userInfo.getAvatar());//评论人头像
                commentVo.setNickname(userInfo.getNickname());//评论人昵称

                //3-3 存储到临时集合
                commentVoList.add(commentVo);
            }
        }
        pageBeanVo.setItems(commentVoList);
        return pageBeanVo;
    }

    public String findMovementPass(List<String> movementId) {
       movementService.MovementPass(movementId);
        return "评审通过";
    }

    public String findMovementReject(List<String> movementId) {
        movementService.MovementReject(movementId);
        return "评审未通过";
    }
}

